package com.aliengen.thingple.driverapp.ui.fragments;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.aliengen.thingple.driverapp.R;
import com.aliengen.thingple.driverapp.data.local.CustomerModel;
import com.aliengen.thingple.driverapp.data.local.PlanGroupModel;
import com.aliengen.thingple.driverapp.ui.adapters.DeliveryPlanListAdapter;
import com.aliengen.thingple.driverapp.ui.listeners.RecyclerTouchListener;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
@EFragment(R.layout.fragment_plans_list)
public class DeliveryPlanListFragment extends BaseFragment {

    private OnListFragmentListener mListener;

    DeliveryPlanListAdapter mAdapter;

    @ViewById
    RecyclerView customers_list;

    @AfterViews
    public void init()
    {
        customers_list.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new DeliveryPlanListAdapter(mListener);
        customers_list.setAdapter(mAdapter);
        customers_list.setHasFixedSize(false);
        customers_list.setItemAnimator(new DefaultItemAnimator());

        customers_list.addOnItemTouchListener(new RecyclerTouchListener(getContext(), customers_list, new RecyclerTouchListener.ClickListener() {

            @Override
            public void onClick(View view, int position) {
                Log.d("DeliveryPlanListFragmen", "onClick");
                PlanGroupModel plangroup = mAdapter.getItem(position);
                mListener.onClickInteraction(plangroup);
            }

            @Override
            public void onLongClick(View view, int position) {
                Log.d("DeliveryPlanListFragmen", "onLongClick");
                PlanGroupModel plangroup = mAdapter.getItem(position);
                mListener.onLongClickInteraction(plangroup);
            }
        }));
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnListFragmentListener) {
            mListener = (OnListFragmentListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnListFragmentListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public DeliveryPlanListAdapter getAdapter()
    {
        return mAdapter;
    }

    public interface OnListFragmentListener {
        public void onClickInteraction(PlanGroupModel item);
        public void onLongClickInteraction(PlanGroupModel item);
    }
}
